package com.soil.code.generation.domain.mybatis.assembler;

import com.soil.code.generation.database.model.ColumnInfo;
import com.soil.code.generation.database.model.TableInfo;
import com.soil.code.generation.domain.mybatis.pojo.MybatisCodeGenerationInfo;
import com.ssy.gaea.common.collection.CollectionUtils;

import java.util.List;

/**
 * @author zeno
 */
public class MybatisCodeAssembler{

  public static MybatisCodeGenerationInfo assembler( TableInfo tableInfo,
    List<ColumnInfo> columnInfos ){

    MybatisCodeGenerationInfo generationInfo = new MybatisCodeGenerationInfo();

    generationInfo.setTableInfo(assemble(tableInfo));

    List<MybatisCodeGenerationInfo.ColumnInfo> infos = CollectionUtils.convert(columnInfos,
      MybatisCodeAssembler::assemble);
    generationInfo.setColumnInfos(infos);

    return generationInfo;
  }

  private static MybatisCodeGenerationInfo.TableInfo assemble( TableInfo tableInfo ){
    MybatisCodeGenerationInfo.TableInfo info = new MybatisCodeGenerationInfo.TableInfo();
    info.setName(tableInfo.getName());
    info.setComment(tableInfo.getComment());
    return info;
  }

  private static MybatisCodeGenerationInfo.ColumnInfo assemble( ColumnInfo columnInfo ){

    MybatisCodeGenerationInfo.ColumnInfo info = new MybatisCodeGenerationInfo.ColumnInfo();

    info.setName(columnInfo.getName());
    info.setJavaType(columnInfo.getJavaType());
    info.setIsPrimaryKey(columnInfo.getIsPrimaryKey());
    info.setIsAutoIncrement(columnInfo.getIsAutoIncrement());
    info.setComment(columnInfo.getComment());

    return info;
  }
}
